package com.live.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.live.model.Apply;
import com.live.model.ApplyStatus;
import com.live.util.DBUtil;
import com.live.util.RowMapper;

public class ApplyDao {
	public int addApply(Apply apply){
		String sql = "insert into applys(name ,companyName, message, type , email,phone,adminId,time,start) values(?,?,?,?,?,?,?,?,?)";
		Object[] param = {apply.getUserName(),apply.getCompanyName(),apply.getMessage(),apply.getProductType(),apply.getUserEmail(),apply.getUserPhone(),apply.getAdminId(),apply.getTime(),apply.getStart() };
		try {
			DBUtil.executeUpdate(sql, param);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public int editApply(Apply apply) {
		String sql = "update applys set remarks=?,start=? where id=?";
		Object[] param = { apply.getRemarks(),apply.getStart(), apply.getId() };
		try {
			DBUtil.executeUpdate(sql, param);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public List<Apply> getPageApply(int pageIndex, int pageSize) {
		String sql = "select a.*,d.name as  adminName,s.name as startName from applys a left join admin d on a.adminId=d.id left join applystatus s  on s.id=a.start  where status=0 order by a.time desc limit ?,?";
		Object[] Param = { (pageIndex - 1) * pageSize, pageSize };
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				List<Apply> data = new ArrayList<Apply>();
				while (rs.next()) {
					Apply apply = new Apply();
					apply.setId(rs.getInt("id"));
					apply.setUserEmail(rs.getString("email"));
					apply.setUserName(rs.getString("name"));
					apply.setUserPhone(rs.getString("phone"));
					apply.setCompanyName(rs.getString("companyName"));
					apply.setMessage(rs.getString("message"));
					apply.setProductType(rs.getString("type"));
					apply.setRemarks(rs.getString("remarks"));
					apply.setAdminId(rs.getInt("adminId"));
					apply.setAdminName(rs.getString("adminName"));
					apply.setStart(rs.getInt("start"));
					apply.setStartName(rs.getString("startName"));
					apply.setTime(rs.getString("time"));
					data.add(apply);
				}
				return data;
			}
		}, Param);
		List<Apply> list = (List<Apply>) result;
		return list;
	}
	public List<Apply> getPageOurselfApply(int adminId,  int pageIndex, int pageSize) {
		String sql = "select a.*,d.name as  adminName ,s.name as startName from applys a left join admin d on a.adminId=d.id left join applystatus s  on s.id=a.start  where a.adminId=? and  status=0 order by a.time desc limit ?,?";
		Object[] Param = {adminId, (pageIndex - 1) * pageSize, pageSize };
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				List<Apply> data = new ArrayList<Apply>();
				while (rs.next()) {
					Apply apply = new Apply();
					apply.setId(rs.getInt("id"));
					apply.setUserEmail(rs.getString("email"));
					apply.setUserName(rs.getString("name"));
					apply.setUserPhone(rs.getString("phone"));
					apply.setCompanyName(rs.getString("companyName"));
					apply.setMessage(rs.getString("message"));
					apply.setProductType(rs.getString("type"));
					apply.setRemarks(rs.getString("remarks"));
					apply.setAdminId(rs.getInt("adminId"));
					apply.setAdminName(rs.getString("adminName"));
					apply.setStart(rs.getInt("start"));
					apply.setStartName(rs.getString("startName"));
					apply.setTime(rs.getString("time"));
					data.add(apply);
				}
				return data;
			}
		}, Param);
		List<Apply> list = (List<Apply>) result;
		return list;
	}
	public int getTotalOurselfCount(int adminId) {
		String sql = "select count(*)totalcount from applys where  adminId=? and status =0";
		Object[] param = {adminId };
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				int totalCount = 0;
				while (rs.next()) {
					totalCount = rs.getInt("totalCount");
				}

				return totalCount;
			}
		}, param);
		return (int) result;
		
	}
	public int getTotalCount() {
		String sql = "select count(*)totalcount from applys where status =0";
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {

				int totalCount = 0;
				while (rs.next()) {
					totalCount = rs.getInt("totalCount");
				}

				return totalCount;
			}
		});
		return (int) result;
	}
	public List<ApplyStatus> getApplyStatus() {
		String sql = "select * from  applystatus";
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				List<ApplyStatus> data = new ArrayList<ApplyStatus>();
				while (rs.next()) {
					ApplyStatus applyStatus = new ApplyStatus();
					applyStatus.setId(rs.getInt("id"));
					applyStatus.setName(rs.getString("name"));
					data.add(applyStatus);
				}
				return data;
			}
		});
		List<ApplyStatus> list = (List<ApplyStatus>) result;
		return list;
	}
}
